package com.jflm.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileUtil {
	private static Log _log = LogFactory.getLog(FileUtil.class);

	/**
	 * ����ļ��Ƿ����
	 * @param String filePathAndName �ļ���ַ 
	 * @return boolean true����
	 * */
	public static boolean isExist(String filePathAndName){
		File myFilePath = new File(filePathAndName);
		//�ж��ļ��Ƿ����
		if (myFilePath.exists()) {
			_log.error(filePathAndName+" the file is exist!");
			return true;
		} 
		_log.error(filePathAndName+" the file not exist!");
		return false;
	}
	
    public static void WriteFile(String fileName, byte data[])
    {
        try
        {
            File f = new File(fileName);
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(data);
            fos.close();
        }
        catch(FileNotFoundException filenotfoundexception) { }
        catch(IOException ioexception) { }
    }

    public static byte[] getFileContent(String fileName)
    {
        byte data[] = (byte[])null;
        FileInputStream fis = null;
        try
        {
            File f = new File(fileName);
            data = new byte[(int)f.length()];
            fis = new FileInputStream(f);
            fis.read(data);
            fis.close();
        }
        catch(IOException e)
        {
            return null;
        }
        return data;
    }
	
	public static void main(String[] args){
		FileUtil.isExist("c:/fqf.txt");
	}
	public String getInputStreamFromString(String str) throws IOException{  
		  
		 InputStream in=new ByteArrayInputStream(str.getBytes("UTF-8"));  
		 BufferedReader bf=new BufferedReader(new InputStreamReader(in,"GBK"));  
	     //����ڽ��ֽ���ת��Ϊ�ַ�����ʱ�� ����ת��  
	     StringBuffer buffer=new StringBuffer();  
	     String line="";  
	     while((line=bf.readLine())!=null){  
	         buffer.append(line);  
	     }  
	       
	    return buffer.toString();  
	
}  
}
